﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Collections;
using System.Diagnostics;
using System.Dynamic;

namespace HandWriting
{
    public partial class Form1 : Form
    {
        Pen pen = new Pen(Color.Black, 16.0f);
        bool mouse_down = false;
        Point mouse_pos;

        public Form1()
        {
            InitializeComponent();

            pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
            Bitmap bm = new Bitmap(pictureBox.Width, pictureBox.Height);
            Graphics g = Graphics.FromImage(bm);
            g.Clear(Color.White);
            g.Dispose();
            pictureBox.Image = bm;
        }

        Process create_python_process(string python_file)
        {
            Process p = new Process();
            p.StartInfo.FileName = "d:/anaconda3/python.exe"; //pytho的安装路径
            p.StartInfo.Arguments = python_file;//python命令的参数 +" com4 57600 password"
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            return p;
        }
        Process CreateProcessTasks(string exe_file)//string str_arguments,
        {
            Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = exe_file;
            process.StartInfo.Arguments = "";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true; 
            return process;
        }
        private void button_identify_Click(object sender, EventArgs e)
        {
            this.Text = "computing ...";
            richTextBox.Text = "";

            Bitmap bm = new Bitmap(pictureBox.Image);
            bm.Save("./test.png", ImageFormat.Png);

            DateTime pre = System.DateTime.Now;

            Process p = create_python_process("./identify_number.py");
            //Process p = CreateProcessTasks("./test.exe");
            p.Start();//启动进程
            richTextBox.Text = p.StandardOutput.ReadToEnd();
            p.WaitForExit();

            TimeSpan ts = System.DateTime.Now.Subtract(pre);
            this.Text = Convert.ToString(ts.TotalSeconds)+" s";
        }

        private void button_clear_Click(object sender, EventArgs e)
        {
            pictureBox.Image.Dispose();
            Bitmap bm = new Bitmap(pictureBox.Width, pictureBox.Height);
            Graphics g = Graphics.FromImage(bm);
            g.Clear(Color.White);
            g.Dispose();
            pictureBox.Image = bm;
        }

        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            mouse_down = true;
            mouse_pos = e.Location;
        }

        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if(mouse_down)
            {
                Bitmap bm = new Bitmap(pictureBox.Image);
                Graphics g = Graphics.FromImage(bm);
                g.DrawLine(pen, mouse_pos, e.Location);
                g.Dispose();
                pictureBox.Image.Dispose();
                pictureBox.Image = bm;
            }
            mouse_pos = e.Location;
        }

        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            mouse_down = false;
            mouse_pos = e.Location;
        }
    }
}
